//
//  BaseEntity.h
//  KoalaKnow
//  
//  Created by sally on 16/1/22.
//  Copyright © 2016年 szjn. All rights reserved.
//

#import "BaseEntity.h"

@implementation BaseEntity

- (instancetype)init {
  self = [super init];
  if (self) {
    self.newRecord = YES;
  }
  return self;
}

- (instancetype)initWithDBSet:(FMResultSet *)set {
  self = [super init];
  if (self) {
    self.newRecord = NO;
  }
  return self;
}

+ (NSArray *)arrayWithDBSet:(FMResultSet *)set {
  NSMutableArray *array = [NSMutableArray array];
  while ([set next]) {
    BaseEntity *entity = [[self alloc] initWithDBSet:set];
    [array addObject:entity];
  }
  return array;
}

- (BOOL)insert {
  return NO;
}

- (BOOL)modify {
  return NO;
}

- (BOOL)delete {
  return NO;
}

- (BOOL)save {
  BOOL result = NO;
  if (self.isNewRecord) {
    result = [self insert];
    if (result) {
      self.newRecord = NO;
    }
  } else {
    result = [self modify];
  }
  return result;
}

+ (BOOL)open {
  return [[DatabaseSetting shareInstance].shareDatabase open];
}

+ (BOOL)close {
  return [[DatabaseSetting shareInstance].shareDatabase close];
}

+ (FMDatabase *)database {
  return [DatabaseSetting shareInstance].shareDatabase;
}

// Method forward
- (BOOL)open {
  return [self.class open];
}

- (BOOL)close {
  return [self.class close];
}

- (FMDatabase *)database {
  return [self.class database];
}

@end
